require "./Graph.rb"
require "./Node.rb"

class Solver
  def initialize(file,debug = false)
    @debug = debug;
    lines = []
    File.open(file,"r") do |f|
      lines = f.readlines
      analyze_lines(lines)
    end
  end

  def analyze_lines(lines)
    @n = Integer(lines[0]);
    if @debug==true
      puts "Found #{@n} graphs!"
    end

    nOfLines = lines.length
    actualLine = 1

    for i in 1..@n
      n_of_nodes = Integer(lines[actualLine])
      graph = Graph.new();
      if @debug==true
        puts "Graph #{i} contains #{n_of_nodes} nodes";
      end
      actualLine += 1
      for j in 1..n_of_nodes
        numbers = lines[actualLine].split(' ')
        if @debug==true
          puts "Found properties for #{numbers[0]}-th node!";
        end
        node = graph.add_node(numbers[0])
        for j in 2..numbers.length-1
          neighbor = graph.add_node(numbers[j]);
          node.add_neighbor(neighbor);
        end
        actualLine += 1;
      end
      puts "graph #{i}"
      while lines[actualLine] != nil && lines[actualLine].include?(" ")
        numbers = lines[actualLine].split(' ')
        if Integer(numbers[0]) != 0
          if @debug==true
            puts "Found command #{numbers[0]} #{numbers[1]}!";
          end
          if Integer(numbers[1])==1
            graph.bfs(numbers[0])
          else
            graph.dfs(numbers[0])
          end
        end
        actualLine += 1
      end
    end
  end
end